package com.supermap.dataservice.thrid.iot;


import com.supermap.dataservice.thrid.iot.wsdl.ServiceLocator;
import com.supermap.dataservice.thrid.iot.wsdl.ServiceSoap12Stub;
import com.supermap.dataservice.util.MD5;
import org.apache.axis.message.SOAPHeaderElement;

import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;

/**
 * @author xiaolu
 * @date 2018/6/7 下午3:01
 */
public class IotWsdlServiceFactory {
    /**
     * 命名空间
     */
    private final static String NS = "http://tempuri.org/";

    private static SOAPHeaderElement authHeader(String userName, String password, boolean isUseMD5) {
        SOAPHeaderElement header = null;
        try {
            header = new SOAPHeaderElement(NS, "AuthHeader");
            header.addChildElement("UserName").addTextNode(userName);
            if (isUseMD5) {
                header.addChildElement("Password").addTextNode(MD5.encode(password));
            } else {
                header.addChildElement("Password").addTextNode(password);
            }
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return header;
    }

    public static ServiceSoap12Stub getServiceSoap12(String userName, String password, boolean isUseMD5) {
        ServiceSoap12Stub stub = null;
        try {
            stub = (ServiceSoap12Stub) new ServiceLocator().getServiceSoap12();
            SOAPHeaderElement header = authHeader(userName, password, isUseMD5);
            stub.setHeader(header);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return stub;
    }

    public static ServiceSoap12Stub getServiceSoap12(String userName, String password) {
        ServiceSoap12Stub stub = null;
        try {
            stub = (ServiceSoap12Stub) new ServiceLocator().getServiceSoap12();
            SOAPHeaderElement header = authHeader(userName, password, true);
            stub.setHeader(header);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return stub;
    }
}
